﻿using System;
using System.Collections.Generic;
using System.Text;
using JQ.SAAS.Entity;
using JQ.SAAS.DTO.OrgUser;
using FastMvc.Crud;
using JQ.SAAS.IService;
using Microsoft.AspNetCore.Mvc;
using FastMvc.Auth;
using System.Threading.Tasks;
using FastMvc.Models;

namespace JQ.SAAS.WebServer.Controllers
{
    /// <summary>
    /// 机构用户管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [PermissionCheck("Admin.Org.User", "机构用户管理")]
    public class OrgUserController : CrudController<sys_user, OrgUserDto, string, OrgUserListRequestDto, OrgUserCreateDto, OrgUserUpdateDto>
    {
        private IOrgUserService _orgUserService;

        public OrgUserController(IOrgUserService orgUserService)
            : base(orgUserService)
        {
            _orgUserService = orgUserService;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <returns></returns>
        [HttpPut("/RestPwd")]
        [PermissionCheck("ResetPwd", "密码重置")]
        public async Task<BaseResult<OrgUserResetPwdDto>> ResetPassWord([FromBody]OrgUserResetPwdDto resetDto)
        {
            resetDto = await _orgUserService.ResetPassWord(resetDto);
            return await SetResultNotNull(resetDto, "重置成功！");
        }

        /// <summary>
        /// 解锁/锁定 用户
        /// </summary>
        /// <returns></returns>
        [HttpPut("Lock")]
        [PermissionCheck("Lock", "用户锁定")]
        public async Task<BaseResult> SetLockState([FromBody] OrgUserLockDto lockDto)
        {
            await _orgUserService.SetLockState(lockDto);
            var cStr = lockDto.iLock == 0 ? "解锁" : "锁定";
            return BaseResult.Success($"{cStr}成功！");
        }

        /// <summary>
        /// 获取用户角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("{sId}/Roles")]
        [PermissionCheck("RoleSet", "角色分配")]
        public async Task<BaseResult<List<OrgUserRoleSetDto>>> GetRoles(string sId)
        {
            List<OrgUserRoleSetDto> datas = await _orgUserService.GetRoles(sId);
            return await SetResultNotNull(datas);
        }

        /// <summary>
        /// 配置用户的角色列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("{sId}/Rolese")]
        [PermissionCheck("RoleSet", "角色分配")]
        public async Task<BaseResult> SetRoles(string sId, [FromBody]List<OrgUserRoleSetDto> roles)
        {
            await _orgUserService.SetRoles(sId, roles);
            return BaseResult.Success("分配完成！");
        }
    }
}
